/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bllInterface;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

/**
 *
 * @author Mrkupi
 */
public class RemoteBufferedOutputStream extends UnicastRemoteObject implements IRemoteBufferedOutputStream
{
    ArrayList<byte[]> bytes = new ArrayList<>();
    ByteArrayOutputStream bostream  =   new ByteArrayOutputStream();
    BufferedOutputStream buffOStream;
    
    private RemoteBufferedOutputStream() throws RemoteException
    {
        buffOStream =   new BufferedOutputStream( bostream );
    }
    
    public static IRemoteBufferedOutputStream CreateUnicastBufferedOutputStream() throws RemoteException
    {
        IRemoteBufferedOutputStream opstream = new RemoteBufferedOutputStream();
        return opstream;
    }

    @Override
    public void write(byte[] b) throws RemoteException, IOException {
        bytes.add(b);
    }

    @Override
    public byte[] toArray() throws RemoteException {
        if(bytes.size() > 0)
        {
            byte[] arr = bytes.get(0);
            bytes.remove(0);
            return arr;
        }
        return null;
    }

    @Override
    public void flush() throws RemoteException, IOException {
        //buffOStream.flush();
    }
}
